﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TMDT_Nhom1A.DTO;
using System.Collections;
namespace TMDT_Nhom1A.DAO
{
    public class Customer_DAO
    {
        private static QL_DoanhNghiepDataContext data = new QL_DoanhNghiepDataContext();

        public static bool RegistryAccount(Customer_DTO user, ref string uid)
        {
            try
            {
                string flag = "";
                uid = "";
                user.Password = MyMembershipProvider.ToHashString(user.Password + user.UserName);

                data.spRegistryAccount(user.NameCompany, user.AddressCompany, user.TaxCode, user.UserName, user.Password, user.DateRegistry, user.NamePresentation, user.Email, user.Phone, user.Mobile, user.Role, ref flag, ref uid);
                if (flag == "0")
                    return false;
                else
                    return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static void GetRoleOfUser(string UserName, ref string UserID, ref string Role)
        {
            data.spRoleOfUser(UserName, ref Role, ref UserID);
        }

        internal static bool activateAccount(int userID, ref string username)
        {
            try
            {
                //lay ra doi tuong users
                USER user = data.USERs.Single(u => u.UserID == userID);

                if (user != null)
                {
                    username = user.Username;
                    user.Activate = true;
                    data.SubmitChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static List<USER> getAllAccount()
        {
            try
            {
                //lay ra doi tuong users
                List<USER> users = data.USERs.ToList();

                return users;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static List<Customer_DTO> getCustomerInfoByUserID(int userID)
        {
            List<Customer_DTO> customerList = new List<Customer_DTO>();
            try
            {
                var customerResult = (from cus in data.CUSTOMERs
                                      join u in data.USERs on cus.UserID equals u.UserID
                                      where cus.UserID == userID
                                      select new
                                      {
                                          UserId = u.UserID,
                                          UserName = u.Username,
                                          Password = u.Password,
                                          Status = u.Status,
                                          Activate = u.Activate,
                                          AddressCompany = cus.Address_Company,
                                          Email = cus.Email,
                                          TaxCode = cus.TaxCode,
                                          NamePresentation = cus.Name_Presentation,
                                          Role = cus.Role,
                                          NameCompany = cus.Name_Company,
                                          Phone = cus.Phone,
                                          Mobile = cus.Mobile,
                                          DateRegistry = cus.Date_Registry
                                      });
                foreach (var item in customerResult)
                {
                    Customer_DTO customer = new Customer_DTO();
                    customer.UserID = item.UserId;
                    customer.UserName = item.UserName;
                    customer.Password = item.Password;
                    customer.Status = (int)item.Status;
                    customer.Activate = (bool)item.Activate;
                    customer.AddressCompany = item.AddressCompany;
                    customer.Email = item.Email;
                    customer.TaxCode = item.TaxCode;
                    customer.NamePresentation = item.NamePresentation;
                    customer.Role = item.Role;
                    customer.NameCompany = item.NameCompany;
                    customer.Phone = item.Phone;
                    customer.Mobile = item.Mobile;
                    customer.DateRegistry = (DateTime)item.DateRegistry;

                    customerList.Add(customer);

                }

                return customerList;

            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool updateUserInfo(Customer_DTO customer)
        {
            try
            {
                USER userResult = (from user in data.USERs
                                   where user.UserID == customer.UserID
                                   select user).Single();
                userResult.Status = (short)customer.Status;
                userResult.Activate = customer.Activate;

                CUSTOMER customerResult = (from cus in data.CUSTOMERs
                                           where cus.UserID == customer.UserID
                                           select cus).Single();

                customerResult.Address_Company = customer.AddressCompany;
                customerResult.Email = customer.Email;
                customerResult.TaxCode = customer.TaxCode;
                customerResult.Name_Presentation = customer.NamePresentation;
                customerResult.Role = customer.Role;
                customerResult.Name_Company = customer.NameCompany;
                customerResult.Phone = customer.Phone;
                customerResult.Mobile = customer.Mobile;
                customerResult.Date_Registry = customer.DateRegistry;

                data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static List<ORDER_B2B> GetBillPaiedBuyOnline(string UserID)
        {
            var list = from o in data.ORDER_B2Bs
                       where o.status == 1 && o.UserID == int.Parse(UserID)
                       select o;
            List<ORDER_B2B> listorderonlinepaied = new List<ORDER_B2B>();
            foreach (var item in list)
            {
                ORDER_B2B orderpaied = new ORDER_B2B();
                orderpaied.OrderB2B_ID = item.OrderB2B_ID;
                orderpaied.OrderedDate = item.OrderedDate;
                orderpaied.OrderTransportDate = item.OrderTransportDate;
                orderpaied.TransportType = item.TransportType;
                orderpaied.OrderTransportDate = item.OrderTransportDate;
                orderpaied.TotalPrice = item.TotalPrice;
                orderpaied.LinkTranport = item.LinkTranport;
                listorderonlinepaied.Add(orderpaied);
            }
            return listorderonlinepaied;
        }

        public static List<ORDER_DETAIL> GetDetailBillPaiedBuyOnline(string OrderB2B_ID, ref string OrderDate)
        {
            List<ORDER_DETAIL> listdetail = new List<ORDER_DETAIL>();
            var list = from o in data.ORDER_DETAILs
                       join ob2b in data.ORDER_B2Bs on o.OrderB2B_ID equals ob2b.OrderB2B_ID
                       select new
                       {
                           o.ElectricDevice_ID,
                           o.Quantity,
                           o.Cost,
                           ob2b.OrderedDate,
                       };
            List<ORDER_DETAIL> listorderdetailonlinepaied = new List<ORDER_DETAIL>();
            foreach (var item in list)
            {
                OrderDate = item.OrderedDate.ToString();
                ORDER_DETAIL orderpaied = new ORDER_DETAIL();
                orderpaied.ElectricDevice_ID = item.ElectricDevice_ID;
                orderpaied.Quantity = item.Quantity;
                orderpaied.Cost = item.Cost;
                listorderdetailonlinepaied.Add(orderpaied);
            }

            return listorderdetailonlinepaied;
        }

        public static List<ORDER_COUPON> GetBillPaiedBuyByCompany(string UserID)
        {
            var list = from o in data.ORDER_COUPONs
                       where o.status == 1 && o.UserID == int.Parse(UserID)
                       select o;

            List<ORDER_COUPON> listordercouponspaied = new List<ORDER_COUPON>();
            foreach (var item in list)
            {
                ORDER_COUPON orderpaied = new ORDER_COUPON();
                orderpaied.OrderCouponID = item.OrderCouponID;
                orderpaied.OrderedDate = item.OrderedDate;
                orderpaied.OrderTransportDate = item.OrderTransportDate;
                orderpaied.TransportType = item.TransportType;
                orderpaied.OrderTransportDate = item.OrderTransportDate;
                orderpaied.TotalPrice = item.TotalPrice;
                listordercouponspaied.Add(orderpaied);
            }
            return listordercouponspaied;
        }

        public static IEnumerable GetInformationOfCustomer(string UserID)
        {
            var list = from c in data.CUSTOMERs
                       where c.UserID == int.Parse(UserID)
                       select c;
            return list.AsEnumerable();
        }

        public static void UpdateInformationOfCustomer(Customer_DTO customer, int flag)
        {
            data.spUpdateInformationCustomer(customer.UserID, customer.AddressCompany, customer.Email, customer.TaxCode, customer.NamePresentation, customer.NameCompany, customer.Phone, customer.Mobile, customer.Password, customer.Role, flag);
        }

        public static List<ORDER_COUPON_DETAIL> GetDetailBillPaiedBuyByCompany(string OrderCouponID, ref string OrderDate)
        {
            List<ORDER_COUPON_DETAIL> listdetail = new List<ORDER_COUPON_DETAIL>();
            var list = from o in data.ORDER_COUPONs
                       join od in data.ORDER_COUPON_DETAILs on o.OrderCouponID equals od.OrderCouponID
                       select new
                       {
                           od.Coupon_ID,
                           od.Quantity,
                           od.Cost,
                           o.OrderedDate,
                       };
            List<ORDER_COUPON_DETAIL> listorderdetailonlinepaied = new List<ORDER_COUPON_DETAIL>();
            foreach (var item in list)
            {
                OrderDate = item.OrderedDate.ToString();
                ORDER_COUPON_DETAIL orderpaied = new ORDER_COUPON_DETAIL();
                orderpaied.Coupon_ID = item.Coupon_ID;
                orderpaied.Quantity = item.Quantity;
                orderpaied.Cost = item.Cost;
                listorderdetailonlinepaied.Add(orderpaied);
            }

            return listorderdetailonlinepaied;
        }

        public static List<USER> searchUserByUsername(string username)
        {
            List<USER> userList = new List<USER>();
            try
            {
                var result = from user in data.USERs
                             where user.Username.Contains(username)
                             select user;

                return result.ToList();
            }
            catch
            {
                return null;
            }
        }
    }
}
